/*
 * ImageOT.java
 *
 * $Id: ImageOT.java,v 1.2 2009-03-02 15:31:23 mario Exp $
 */
package org.ceteca.explica.core.common;

import java.io.Serializable;

import org.ceteca.explica.core.InterfaceExplica;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;

/**
 * Store class to hold the data of an application image resource.
 * <br/>
 * Responsibilities:
 * <li>
 * 	<ul> Store of image data for exchanging data among presentation, 
 * business and data access layers. </ul>
 * </li>.
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2008 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ImageOT extends BaseVO implements Serializable {
	/**
	 * Internal id of the image.
	 */
	private long idImage;
	/**
	 * Path of the image file.
	 */
	private String path;
	/**
	 * Version of the image used by Hibernate for optimistic concurrency control.
	 */
	private Integer version = new Integer(0);
	/**
	 * Instance of image renderized from the image file.
	 */
	private Image renderizedImage;
	/**
	 * Contents of the image file.
	 */
	private byte[] contents;
	
	private ImageData imgData;
	
	private static final long serialVersionUID = 10000008;
	
	/**
	 * Default ImageOT constructor.
	 */
	public ImageOT() {
	}
	
	/**
	 * Gets the version of the image used by Hibernate for optimistic concurrency control.
	 * @return Integer, version of the image.
	 */
	public Integer getVersion() {
		return version;
	}
	/**
	 * Sets up the version of the image used by Hibernate for optimistic concurrency control.
	 * @param version Integer, version of the image.
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * Gets the path of the image.
	 * @return String, description of the image.
	 */
	public String getPath() {
		return path;
	}
	/**
	 * Sets up the path of the image.
	 * @param path String, path of the image.
	 */
	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * Gets the identifier of the image.
	 * @return long, identifier of the image.
	 */
	public long getIdImage() {
		return idImage;
	}
	/**
	 * Sets up the identifier of the image.
	 * @param idImage long, identifier of the image.
	 */
	public void setIdImage(long idImage) {
		this.idImage = idImage;
	}

	/**
	 * Gets the renderized image loaded from the image file.
	 * @return Image, renderized image.
	 */
	public Image getRenderizedImage() {
		return renderizedImage;
	}
	/**
	 * Sets up the renderized image loaded from the image file
	 * @param renderizedImage Image, renderized image.
	 */
	public void setRenderizedImage(Image renderizedImage) {
		this.renderizedImage = renderizedImage;
	}
	
	public byte[] getContents() {
		return contents;
	}
	public void setContents(byte[] contents) {
		this.contents = contents;
	}
	
	public ImageData getImgData() {
		return imgData;
	}
	public void setImgData(ImageData imgData) {
		this.imgData = imgData;
	}

	/**
	 * Implementation of the equals method provided by the serializable interface.
	 */
	public boolean equals(Object other) {
        if (this == other) 
        	return true;
        if ( !(other instanceof ImageOT) ) 
        	return false;

        final ImageOT image = (ImageOT) other;

        if ( image.getPath() == null )
        	return this.getPath() == null;
        else if ( !image.getPath().equals( this.getPath() ) ) 
        	return false;

        return true;
    }

	/**
	 * Implementation of the hashCode method provided by the serializable interface.
	 */
    public int hashCode() {
        int result;
        if (this == null || this.getPath() == null)
        	result = 0;
        else
        	result = this.getPath().hashCode();
        return result;
    }
    
	/**
	 * Implementation of the toString() method to show the data of the ExtFieldOT
	 * formatted.
	 */
	public String toString() {
		StringBuffer result = new StringBuffer();
		try {
			result.append("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			result.append("\nID: ");
			result.append(this.idImage);
			result.append("\nPATH: ");
			result.append(this.path);
			result.append("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		}
		catch (Exception ex) {
			System.out.println("Error formatting ImageOT!");
			ex.printStackTrace();
			return null;
		}
		
		return result.toString();
	}
    
	@Override
	public boolean equals(Long id) {
		return id != null && this.idImage == id.longValue();
	}
	
	@Override
	public long getInternalId() {
		return this.idImage;
	}

	@Override
	public String getInternalValue() {
		return this.path;
	}
	
	@Override
	public String getEntityId() {
		return null;
	}

	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ImageOT.java,v 1.2 2009-03-02 15:31:23 mario Exp $";
	}	
}
